package com.xm.dao.impl;

import com.xm.dao.OrderGoodsDao;
import com.xm.po.OrderGoods;
import com.xm.util.DButil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2020-08-07-15:30
 * @description 订单商品数据操作接口实现类
 */
public class OrderGoodsDaoImpl implements OrderGoodsDao {
    @Override
    public boolean add(OrderGoods orderGoods) {
        return DButil.insert(orderGoods);
    }

    @Override
    public List<OrderGoods> getOrderGoodsByNo(Integer orderId) {
        Connection conn = DButil.getConnection();
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        List<OrderGoods>list= new ArrayList<>();
        String sql="select * from ordergoods where orderId=?";
        try {
            pstmt=conn.prepareStatement(sql);
            rs=pstmt.executeQuery();
            list= (List<OrderGoods>) DButil.getRsOne(OrderGoods.class,rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            DButil.close(rs,pstmt,conn);
        }
        return null;
    }

    @Override
    public List<OrderGoods> getAll() {
        List<OrderGoods>list= (List<OrderGoods>) DButil.queryAll(OrderGoods.class);
        return list;
    }

    @Override
    public boolean orederUpdate(OrderGoods orderGoods) {
        return DButil.update(orderGoods);
    }

    @Override
    public OrderGoods getById(Integer id) {
        Connection conn = DButil.getConnection();
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        OrderGoods orderGoods = new OrderGoods();
        String sql="select * from ordergoods where id = ?";
        try {
            pstmt= conn.prepareStatement(sql);
            pstmt.setObject(1,id);
            rs=pstmt.executeQuery();
            orderGoods = (OrderGoods) DButil.getRsOne(OrderGoods.class,rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            DButil.close(rs,pstmt,conn);
        }
        return orderGoods;
    }

    @Override
    public boolean orderDelete(Integer id) {
        Connection conn = DButil.getConnection();
        PreparedStatement pstmt=null;
        String sql="delete from ordergoods where id=? ";
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setObject(1,id);
            return pstmt.executeUpdate()>0;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DButil.close(null,pstmt,conn);
        }
        return false;
    }
}
